﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dathuc;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private List<DaThuc> array;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            array = new List<DaThuc>();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtNhap.Text == "")
                return;
            array.Add(new DaThuc(txtNhap.Text));
            lstbDathuc.Items.Add("[!]" + "    "+ txtNhap.Text);
            txtNhap.Text = "";
        }

        private void btnThuchien_Click(object sender, EventArgs e)
        {
            List<int> danhsachchon = new List<int>();
            foreach (object o in lstbDathuc.SelectedItems)
                danhsachchon.Add(lstbDathuc.Items.IndexOf(o));
            if (danhsachchon.Count < 2)
            {
                MessageBox.Show("Vui lòng chọn hai đa thức","Thông báo");
                return;
            }
            if(rdbtnCong.Checked)
                txtNhap.Text = "Ket qua: " + (array[danhsachchon[0]] + array[danhsachchon[1]]).ToString();
            if (rdbtnNhan.Checked)
                txtNhap.Text = "Ket qua: " + (array[danhsachchon[0]] * array[danhsachchon[1]]).ToString();
        }

        private void lstbDathuc_Click(object sender, EventArgs e)
        {
            //chi duoc thuc hien phep tinh khi co hai bieu thuc duoc chon va da chon phep tinh can thuc hien
            Kiemtra();
        }

        private void Kiemtra()
        {
            int dem = 0;
            foreach (object o in lstbDathuc.SelectedItems)
                ++dem;
            if (dem < 2)
            {
                btnThuchien.Enabled = false;
            }
            else
                if ((dem == 2) && rdbtnCong.Checked || rdbtnNhan.Checked)
                btnThuchien.Enabled = true;
            if (dem == 1)
                btnSua.Enabled = true;
            else
                btnSua.Enabled = false;
        }

        private void txtNhap_TextChanged(object sender, EventArgs e)
        {
            if (((TextBox)sender).Text != "")
                btnThem.Enabled = true;
            else
                btnThem.Enabled = false;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            string chuoi = lstbDathuc.SelectedItem.ToString();
            chuoi = chuoi.Substring(chuoi.IndexOf(']')+1).Trim();
            lstbDathuc.Items.Remove(lstbDathuc.SelectedItem);
            txtNhap.Text = chuoi;
        }

        private void rdbtnNhan_Click(object sender, EventArgs e)
        {
            Kiemtra();
        }

        private void rdbtnCong_Click(object sender, EventArgs e)
        {
            Kiemtra();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            array = new List<DaThuc>();
            lstbDathuc.Items.Clear();
            txtNhap.Text = "";
        }
    }
}
